<template>
    <div id="addFill">
        <div class="jx-content">
            <div class="topcoumt">
                    <div class="search-box fl">
                        <el-form :inline="true"  :model="Form" label-width="120px" class="fl">
                            <el-form-item label="关键字">
                                <el-input v-model="Form.name" placeholder="请输入关键字"></el-input>
                            </el-form-item>
                        </el-form> 
                        <button class="button-style" @click="searchFn" style="margin-top:3px">搜 索</button> 
                    </div>
                    <div class="fr btn-box">
                        <button class="button-style" @click="addFn()" >添 加</button>
                        <button class="button-style" @click="deleteFn()">批 量 删 除</button>
                        <button class="button-style" @click="listProp()" >批 量 导 入</button>
                        <a class="button-style" @click="DownLoad()" >模 板 下 载</a>
                    </div>
            </div>
            <div class="table">
               <el-table :data="tableData" style="width: 100%">
                    <el-table-column  type="selection" width='100'></el-table-column>
                    <el-table-column prop="name" label="教师名称"></el-table-column>
                    <el-table-column prop="number" label="工号" ></el-table-column>
                    <el-table-column prop="userSex" label="性别">
                        <template slot-scope="scope">
                            {{ scope.row.userSex == '1' ? '男' : '女' }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="name" label="手机号"></el-table-column>
                    <el-table-column prop="email" label="邮箱"></el-table-column>
                    <el-table-column  label="操作">
                         <template slot-scope="scope">
                            <el-button type="text" size="small"  @click="repeatFn(scope.row.id)">修改</el-button>
                            <el-button type="text" size="small"  @click="repeatCode(scope.row.id)">初始密码</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
             <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-size="pagesize"
                    layout="total, prev, pager, next, jumper"
                    :total="totalCount"
             ></el-pagination>
               <el-dialog  title="添加用户" :label-position="labelPosition" :visible.sync="dialogVisible" width="430px"  @close="closeDialog">
                <el-form ref="formlist" :inline="true"  :rules="rules" :model="formlist" >
                    <el-form-item label="姓  名 :" prop="name">
                        <el-input v-model="formlist.name" placeholder="请输入姓名"></el-input>
                    </el-form-item>
                    <el-form-item label="工  号 :" prop="number">
                        <el-input v-model="formlist.number" placeholder="请输入工号"></el-input>
                    </el-form-item>
                     <el-form-item label="手机号 :"  >
                        <el-input v-model="formlist.telephone" placeholder="请输入手机号"></el-input>
                    </el-form-item>
                    <el-form-item label="邮  箱 :" >
                        <el-input v-model="formlist.email" placeholder="请输入邮箱"></el-input>
                    </el-form-item>
                    <el-form-item label="性  别 :" >
                        <el-radio-group v-model="formlist.gender">
                            <el-radio :label="1">男</el-radio>
                            <el-radio :label="2">女</el-radio>
                        </el-radio-group>
                    </el-form-item>
                     <el-form-item label="密  码 :" prop="passWord">
                        <el-input v-model="formlist.passWord" placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码 :" prop="checkPass" >
                        <el-input v-model="formlist.checkPass" placeholder="请输入工号"></el-input>
                    </el-form-item>
                </el-form>
                <div class="btn-center" style="text-align: center;">
                    <button class="button-style" @click="submit('formlist')" >保 存</button>
                </div>
             </el-dialog>
             <el-dialog class="common-tool-dialog" title="导入教师" :visible.sync="dialogFormVisible2" width="500px" @close="closeTeacher" @clsoe="closeTeacherDialog">
                <el-form :model="form2" :rules="rules2" ref="form2">
                    <el-form-item prop="versionFile" class="version-file">
                        <el-button type="primary">上传文件<input id="version-file-input" type="file" multiple="multiple" auto-complete="off" @change="getFile"></el-button>
                        <p style='margin-top:10px;' v-if="form2.versionFile.length>0">已选: {{form2.versionFile}}</p>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:center;">
                    <el-button @click="closeTeacher">取消</el-button>
                    <el-button type="primary" @click="saveForm('form2')">确认</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    import $ from "jquery";
    export default {
        data () {
            //邮箱
            var validateEmail = (rule, value, callback) => {
                    if (value === '') {
                    callback(new Error('请正确填写邮箱'));
                    } else {
                    if (value !== '') { 
                        var reg=/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
                        if(!reg.test(value)){
                        callback(new Error('请输入有效的邮箱'));
                        }
                    }
                    callback();
                    }
            }
            //手机号验证
            var validPhone = (rule, value,callback)=>{
                if (!value){
                    callback(new Error('请输入电话号码'))
                }else {
                    if (value !== '') { 
                        var reg=/^1[3456789]\d{9}$/;
                        if(!reg.test(value)){
                        callback(new Error('请输入正确的11位手机号码'));
                        }
                    }
                    callback();
                    }
            }
            //密码验证
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'))
                } else {
                    if (this.formlist.checkPass !== '') {
                        this.$refs.formlist.validateField('checkPass')
                    }
                        callback()
                    }
            }
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'))
				} else if (value !== this.formlist.passWord) {
					callback(new Error('两次输入密码不一致!'))
				} else {
					callback()
				}
            }
            return {
                Form:{
                    name:'',
                },
                labelPosition: 'right',
                dialogVisible:false,
                dialogFormVisible2:false,
                currentPage: 1,
                pagesize: 10,
                totalCount: 1000,
                tableData: [],
                multipleSelection:[],
                formlist:{
                     number:'',
                     name:'',
                     gender: 1,
                     checkPass:'',
                     passWord:'',
                     telephone:'',
                     email:'',    
                },
                 form2: {
                    fileName: '',
                    versionFile: ''
                },
                 rules2: {
                    versionFile: [
                        { required: true, message: '请上传文件', trigger: 'blur' }
					]
                },
                 rules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
					],
                    number: [
                        { required: true, message: '请输入工号', trigger: 'blur' }
                    ],
                    role: [
                        { required: true, message: '请输入角色', trigger: 'blur' }
                    ],
                    telephone: [
                        { required: true, message: '请输入联系方式', trigger: 'blur' },
                        { required: true, validator: validPhone, trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱', trigger: 'blur' },
                        { required: true, validator: validateEmail, trigger: 'blur' }
					],
                    passWord: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
						{ required: true, validator: validatePass, trigger: 'blur' }
					],
					checkPass: [
                        { required: true, message: '请输入确认密码', trigger: 'blur' },
						{ required: true, validator: validatePass2, trigger: 'blur' }
                    ]
                },
            }

        },
        methods: {
             //关闭导入教师弹窗
            closeTeacherDialog(){
                this.$refs.form2.resetFields()
            },
            //全选
			handleSelectionChange(val) {
				let valArr = []
				for(let i = 0; i < val.length; i++){
					valArr.push(val[i].id)
				}
				this.multipleSelection = valArr
            },
            //关闭弹窗
            closeDialog(){
                this.$refs.formlist.resetFields()
            },
            //搜索
            searchFn(){

            },
            //添加
            addFn(){
                 this.dialogVisible=true
            },
            //批量导入
            listProp(){
                 this.form2.fileName = ''
                 this.form2.versionFile = ''
                 this.dialogFormVisible2=true
            },
            //批量删除
            deleteFn(){

            },
            //模板下载
            DownLoad(){

            },
             //分页
			handleSizeChange: function (size) {
				this.pagesize = size;
			},
			handleCurrentChange: function(currentPage) {
                this.currentPage = currentPage
                //this.init()
            },
             //上传文件
            getFile($event) {
                let fileType = $event.target.files[0].name
                    fileType = fileType.substring((fileType.lastIndexOf('.') + 1), fileType.length)
                if(fileType != 'xls' && fileType != ".xlsx"){
                    this.$message.error('请上传xls格式的Excel文件!')
                    return
                }
                this.form2.fileName = $event.target.files[0]
                this.form2.versionFile = $event.target.files[0].name
            },
            //关闭弹窗
            closeTeacher(){
                this.dialogFormVisible2 = false
                $('#version-file-input').val('')
            },
        }
    }
</script>
<style scoped>
.el-form-item>>>.el-input__inner{
    width: 200px;
}
.el-form-item>label{
    text-align: right
}
#version-file-input{
    position: absolute;
    left: 0;
    opacity: 0;
    height: 45px;
    top: 0px;
}
.el-form--inline>>>.el-form-item__label {
    text-align: right;
    float: left;
    display: inline-block;
    width: 105px !important;
}
.el-form >>>.el-input__inner{
     width: 200px;   
}
</style>
<style lang="scss" scoped>
 @import "./../../../styles/layout/color_variables";
  #addFill {
    width: $width;
    margin: 20px auto;
    color: #727272;
      .topcoumt{
       overflow: hidden;
        .el-input .el-input__inner{
            width: 200px;
        }
    }
    .btn-box{
        .button-style{
            margin-right: 10px;
        }
    }
}

</style>
